package com.mobimagic.lockscreen.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

public class Wave {
    private static final float WAVE_LENGTH_D = 0.35f;
    private ValueAnimator valueAnimator;
    private Path path;
    private int waveLength;
    private int peak;

    private int color;
    private int width, height;
    private int levelY = 0;
    private float animateValue;
    private View v;
    /**
     * 因为只有三个生命周期，建议waveX在0-1之间
     */
    private float waveX;

    private Rect dirty = new Rect();

    public void setColor(int color) {
        this.color = color;
    }

    public Wave(int peak, float waveX, View v) {
        path = new Path();
        this.peak = peak;
        this.v = v;
        this.waveX = waveX;
    }

    public void setLevelY(int levelY) {
        this.levelY = levelY;

    }

    private void start() {
        if (MobileChargingView.DEBUG) {
            Log.d(MobileChargingView.TAG, "wave start!");
        }
        valueAnimator = ValueAnimator.ofFloat(waveX, waveX + 1);
        if (waveX > 0) {
            valueAnimator.setDuration(2000);
        } else {
            valueAnimator.setDuration(3500);
        }
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float) animation.getAnimatedValue();
                animateValue = (float) (value - Math.floor(value));
                if (v != null) {
                    dirty.set(0, levelY - peak, 0 + width, levelY + peak);
                    v.invalidate(dirty);
                }

            }
        });
        valueAnimator.start();
    }

    private void stop() {
        if (MobileChargingView.DEBUG) {
            Log.d(MobileChargingView.TAG, "wave stop!");
        }
        if (valueAnimator != null) {
            valueAnimator.cancel();
            valueAnimator = null;
        }
    }

    public void generatePath(int width, int height) {
        this.width = width;
        this.height = height;
        path.reset();
        waveLength = (int) (width * WAVE_LENGTH_D);
        int x = 0;
        int peak = (int) (this.waveX == 0 ? this.peak : 1 * this.peak * 0.5f);
        path.moveTo(x, 1 * this.peak);
        path.lineTo(x, 0);
        //向下
        path.quadTo(x + 1 * waveLength, peak, x + 2 * waveLength, 0);
        path.quadTo(x + 3 * waveLength, -peak, x + 4 * waveLength, 0);

        path.quadTo(x + 5 * waveLength, peak, x + 6 * waveLength, 0);
        path.quadTo(x + 7 * waveLength, -peak, x + 8 * waveLength, 0);

        //        path.quadTo(x + 9 * waveLength, peak, x + 10 * waveLength, 0);
        //        path.quadTo(x + 11 * waveLength, -peak, x + 12 * waveLength, 0);

        path.lineTo(x + 8 * waveLength, 1 * this.peak);
        path.lineTo(x, 1 * this.peak);
        path.close();
    }

    public void draw(Canvas canvas, Paint paint) {
        draw(canvas, paint, color);
    }

    private void draw(Canvas canvas, Paint paint, int color) {

        paint.setStyle(Style.FILL);
        float x = ((-animateValue)) * (4 * width * WAVE_LENGTH_D);
        //        float x = ((-animateValue)) * (waveLength * 9 - width);
        canvas.save();
        canvas.clipRect(0, levelY - peak, 0 + width, levelY + peak);
        canvas.translate(x, levelY);
        paint.setColor(color);
        canvas.drawPath(path, paint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(0, levelY + peak, width, height);
        canvas.drawRect(0, levelY + peak, width, height, paint);
        canvas.restore();

    }

    public void drawW(Canvas canvas, Paint paint) {
        draw(canvas, paint, Color.WHITE);
    }

    public int getPeak() {
        return peak;
    }

    public void setPeak(int peak) {
        this.peak = peak;
    }

    public void onStop() {
        if (MobileChargingBGView.SAVE_MODE) {
            stop();
        }
    }

    public void onStart() {
        if (MobileChargingBGView.SAVE_MODE) {
            start();
        }
    }

    public void onAttachedToWindow() {
        if (!MobileChargingBGView.SAVE_MODE) {
            start();
        }

    }

    public void onDetachedFromWindow() {
        if (!MobileChargingBGView.SAVE_MODE) {
            stop();
        }
    }
}
